Skip to content

feat(core)!: async hooks, hook timeouts, input rewriting and subagent hooks - #144

Merged
gopaljigaur merged 2 commits into
mainfrom
feat/more-hooks
Oct 19, 2026
Merged

gopaljigaur merged 2 commits into
mainfrom
feat/more-hooks

Conversation

@gopaljigaur

@gopaljigaur gopaljigaur commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

Adds the hook points and timeouts from #72 that were still missing, in the shapes the hooks and middleware already use.

What changed

  • Async hooks. Any AgentHooks method may be async def; it is awaited. Detection is inspect.iscoroutinefunction, as Pydantic AI does for history processors. The base methods are typed MaybeAwaitable[...] so async overrides type-check.
  • Input rewriting (breaking). on_run_start may return text or content parts to replace the run's input, the way before_tool returns new arguments (and LangChain's before_agent returns a state update). None keeps the input. The replacement goes through the same check as run input, and it is what the model and the session get. No new hook name. Its return value used to be ignored, so a hook or mock that returns something must now return None. The hook and the check run inside the run's error handling, so on_run_error fires when either fails.
  • Subagent hooks. on_subagent_start(agent_path, input_message) and on_subagent_end(agent_path, result) fire on the calling agent's hooks around each nested AgentTool run; on_subagent_end gets the RunResult however the run ended. Only the direct caller's hooks get them, so a shared hooks object is not called twice for deeper nesting. Named after the existing subagent terms (max_subagent_depth, cyclops.core.subagents); the OpenAI Agents SDK's closest pair is on_agent_start/on_agent_end.
  • AgentConfig.hook_timeout. Seconds each async hook call may run, beside tool_timeout, using asyncio_timeout from cyclops/utils/compat.py. A hook past it is cancelled and raises TimeoutError, and the run treats it as a hook that raised. This keeps on_tool_start fail closed (a slow approval never allows a call) and needs no option. Plain hooks run inline as before and are not bounded, since asyncio cannot interrupt blocking code; the guide says to write a hook as async def to bound it.
  • All hook call sites go through one helper, _call_hook.
  • Docs: hooks guide, agents and subagents guides, and the API reference, in docs/ and on the website.

Issue items that already existed

  • Compaction hook: on_compaction(summary, tokens_before, tokens_after) came with feat: summarize older messages near the model's input limit #118, and the summarizing completion already fires on_llm_start/on_llm_end. Nothing added.
  • Nested runs: before this PR, AgentTool runs already sent their events to the outermost run with agent_path, and the caller's hooks saw them as an ordinary tool call (on_tool_start/on_tool_end). No hook saw the nested run itself.
  • Async: ToolMiddleware and guards were already async; AgentHooks was sync only.
  • Before-run rewriting, async AgentHooks and timeouts did not exist.

Tests

tests/test_hooks.py: replacing the input (text, and context added as parts from an async hook); a replacement that fails the input check and a timed out on_run_start, both firing on_run_error; async hooks awaited in order; timeouts on async hooks, including a timed out on_tool_start whose tool never runs; plain hooks running inline under hook_timeout; hooks within their timeout; cancelling a run during a hook; base hooks sending the same requests as no hooks; and subagent runs (completed, failed, cancelled, and nested two deep) seen by the caller's hooks. Mocked AgentHooks in existing tests now set on_run_start.return_value = None, since a mock's return value would otherwise replace the input.

uv run pytest -n 4 (732 passed, also on 3.10), uv run pre-commit run --all-files and uv run mypy cyclops pass.

Closes #72

@gopaljigaur gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every hook call site on main goes through _call_hook (all 15), and the order is unchanged. The conflict resolutions in run.py, fallback.py and compaction.py change only the hook lines. Subagent hooks pair with current_tool_call_id() for parallel calls, and cancellation behaves on 3.10. The new tests fail on main except the base hooks guard. Checks pass: pytest -n 4 (728), pre-commit, mypy, the site build, CI, and the branch is on main. Four findings:

  1. With hook_timeout set, plain hooks run in threads: TelemetryHooks then records no spans, and plain hooks run concurrently. Applying the timeout to async hooks only removes both (inline on hooks.py).
  2. A replacement that fails the input check skips on_run_error (inline on run.py).
  3. Using on_run_start's return value changes existing behaviour; the commit should be marked breaking (inline on hooks.py).
  4. Missing tests: a timed out on_tool_start, cancellation during a hook, and a cancelled subagent's end hook (inline on tests/test_hooks.py).

Comment thread cyclops/core/hooks.py Resolved
Comment thread cyclops/core/run.py Resolved
Comment thread cyclops/core/hooks.py Resolved
Comment thread tests/test_hooks.py Resolved
… hooks

Every AgentHooks method may now be async def and is awaited.
on_run_start may return text or content parts to replace the run's input,
checked as run input is. on_subagent_start and on_subagent_end fire on the
calling agent's hooks around each nested subagent run.
AgentConfig.hook_timeout caps each hook call; a hook past it raises
TimeoutError, as a hook that raises does.

BREAKING CHANGE: the return value of AgentHooks.on_run_start is no longer
ignored. Any value other than None replaces the run's input, so a hook (or a
mock standing in for AgentHooks) that returns something must return None to
keep the input.
hook_timeout no longer moves plain hooks to a worker thread, which lost the
context variables they set and ran them concurrently; plain hooks run inline
and only async hooks are bounded. on_run_start and the check of its
replacement now run inside the run's error handling, so on_run_error fires
when either fails.
@gopaljigaur gopaljigaur changed the title feat(core): async hooks, hook timeouts, input rewriting and subagent hooks feat(core)!: async hooks, hook timeouts, input rewriting and subagent hooks Oct 15, 2026

@gopaljigaur gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings.

@gopaljigaur
gopaljigaur merged commit 18daec6 into main Oct 19, 2026
13 checks passed
@gopaljigaur
gopaljigaur deleted the feat/more-hooks branch October 19, 2026 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More hook points and hook timeouts

1 participant